package com.jacquesb.mpl;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*; 
import android.view.View.OnClickListener;
import android.util.Log;

public class MainActivity extends Activity
 implements View.OnClickListener
{
    EditText e = null;
    Button b = null;

    int wordsize=8;
    int i;
    int pc, r0, r1, rc, ru, rd;
    int t;
    int state;
    
    int memsize = 0xF000;
    int mem[] = new int[memsize];
    String mplo = ";;;;;;;;;#7000d#H{#,#6000?}#6000H{#7001r,#7003wQ}Q";
	
    public void trace (String s) 
    {
        // Toast.makeText(this, s, Toast.LENGTH_SHORT).show(); 
	Log.d ("mpl", s);
    }	
	
    public void mpli ()
    {
       boolean cont = true;
       char instr;
       while (cont)
       {
           instr = (char)(mem[pc++]);
           trace ("pc="+new Integer(pc).toString()+" instr="+new Character(instr).toString());
           if (state == 1)
           {
               if (instr == '}')
                   state = 0;
               else
                   mem[rc++] = (int)instr;
           }
           else if (state == 0)
           {
             switch (instr)
             {
               case ',': r1=r0; break;
               case 'X': t=r0; r0=r1; r1=t; break;
               case 'p': r0=pc; break;
               case '?': if (r1==0) { r1=pc; pc=r0; } break;
               case 'R': case 'r': r0=mem[r0]; break;
               case 'W': case 'w': mem[r0]=r1; break;
               case '#': r0=0; break;

               case '0': r0=r0*16; break;
               case '1': r0=r0*16+1; break;
               case '2': r0=r0*16+2; break;
               case '3': r0=r0*16+3; break;
               case '4': r0=r0*16+4; break;
               case '5': r0=r0*16+5; break;
               case '6': r0=r0*16+6; break;
               case '7': r0=r0*16+7; break;
               case '8': r0=r0*16+8; break;
               case '9': r0=r0*16+9; break;
               case 'A': r0=r0*16+10; break;
               case 'B': r0=r0*16+11; break;
               case 'C': r0=r0*16+12; break;
               case 'D': r0=r0*16+13; break;
               case 'E': r0=r0*16+14; break;
               case 'F': r0=r0*16+15; break;

               case '~': r0 = ~r0; break;
               case '+': r0=r0+r1; break;
               case '-': r0=r0-r1; break;
               case '*': r0=r0*r1; break;
               case '/': r0=r0/r1; break;
               case '%': r0=r0%r1; break;
               case '&': r0=r0&r1; break;
               case '^': r0=r0^r1; break;
               case '|': r0=r0|r1; break;
               case '<': r0=r0<<r1; break;
               case '>': r0=r0>>r1; break;

               case 'H': rc=r0; break;
               case 'h': r0=rc; break;
               case '{': state = 1; break;
               case '}': mem[rc++] = (int)(instr); break;
               case '_': mem[rc++]=r0; break;
               case 's': r0=wordsize; break;

               case 'e': break;

               case 'u': r0=ru; break;
               case 'U': ru=r0; break;
               case 'J': t=r0; 
                         r0=mem[t]; 
                         r1=mem[t+wordsize]; 
                         pc=mem[t+2*wordsize]; 
                         break;

               case 'd': t=rd; rd=r0; r0=t; break;

               case 'Q': cont=false; break; 

               case ';': case ' ': case '\n': case '\r': case '\t': break;

               default: mem[ru]=r0; 
                        mem[ru+wordsize]=r1; 
                        mem[ru+2*wordsize]=pc; 
                        pc=ru+3*wordsize; 
                        break;
             }
          }
       } 
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
		
        e = (EditText) findViewById(R.id.edit1);
        b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(this);

        trace ("mplo="+mplo);
        int iq=(int)'Q';
        String sq=new Integer(iq).toString();
        trace("sq="+sq);
        for (i=0; i<mplo.length(); i++)
        {
            mem[i] = (int)(mplo.charAt(i));
            trace("mem["+new Integer(i).toString()+"]="+new Integer(mem[i]).toString());
        }
        pc = 0;
        r0 = 0;
        r1 = 0;
        rc = 0;
        ru = 0;
        rd = 0;
        mpli();

    }
	
    @Override
    public void onClick (View v)
    {
        // e.setText (e.getText().toString()+ e.getText().toString());
        String s = e.getText().toString();
		trace("s="+s+".");
        for (i=0; i<s.length(); i++)
            mem[rd+i] = (char)(s.charAt(i));
        mem[rd+s.length()] = 0;
		pc=0;
        mpli();
        i = 0;
        String t = "";
        while (mem[rd+i] != 0)
		{
			trace("rd="+new Integer(rd).toString()+" mem[rd+"+new Integer(i).toString()+"]="+new Integer(mem[rd+i]).toString());
            // t = t + new Character((char)(mem[rd+i])).toString();
			t = t + (char)(mem[rd+i]);
	        i = i+1;
		}
		trace("t="+t+".");
        e.setText(t);
    }
}
